package project;

import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.ElementIterator;
import javax.swing.text.SimpleAttributeSet;
import com.google.api.translate.Language;
import com.google.api.translate.Translator;
import java.sql.Date;
import java.sql.Time;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.JOptionPane;

/**
 * Author: Raoux Etienne
 * Student login: jc218934
 * Student number: 12448190
 * Date: Jul 31, 2010
 * Time: 9:46:32 AM
 * email: etienne.raoux@my.jcu.edu.au
 * alt email: jc218934@gmail.com
 */
public class urlExtractorFromDoc
{
    Document auctionDoc;
    private ElementIterator iterator;
    private Element element;
    private SimpleAttributeSet simpleAttributeSet;
    HtmlSourceReaderToDoc reader;
    private Vector<String> itemList;
    private Vector<String> itemHistoryList;



    private Vector <String> itemImageUrlList;//    extractImageUrlFromString(auctionUrl);
    private Vector <String> itemTitleList;//    extractTitleFromString(auctionUrl);
    private Vector <String> itemDescriptionUrlList;//    extractItemDescriptionUrlFromString(auctionUrl);
    private Vector <String> sellerUrlList;//    extractSellerUrlFromString(auctionUrl);
    private Vector <String> sellerList;//    extractSellerFromString(auctionUrl);
    private Vector <String> itemPriceList;//    extractPriceFromString(auctionUrl);
    private Vector <String> itemBiddingHistoryUrlList;//    extractItemBiddingHistoryUrlFromString(auctionUrl);

    public urlExtractorFromDoc(String url)
    {
        itemImageUrlList = new Vector <String>();
        itemTitleList = new Vector <String>();
        itemDescriptionUrlList = new Vector <String>();
        itemDescriptionUrlList = new Vector <String>();
        sellerUrlList = new Vector <String>();
        sellerList = new Vector <String>();
        itemPriceList = new Vector <String>();
        itemBiddingHistoryUrlList = new Vector <String>();
        try
        {
            

            reader = new HtmlSourceReaderToDoc();
            itemList = reader.readProducts(url);
            for (String auctionUrl:itemList)
            {
                extractImageUrlFromString(auctionUrl);
                extractTitleFromString(auctionUrl);
                extractItemDescriptionUrlFromString(auctionUrl);
                extractSellerUrlFromString(auctionUrl);
                extractSellerFromString(auctionUrl);
                extractPriceFromString(auctionUrl);
                extractItemBiddingHistoryUrlFromString(auctionUrl);
                System.out.println("\n");
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
        }

    }

    private void extractImageUrlFromString(String str)
    {
        String start = "<img src=\"";
        String end = "\"";
        str = str.substring(str.indexOf(start)+start.length());
        str = str.substring(0, str.indexOf(end));
        System.out.println("image url= "+str);
        itemImageUrlList.add(str);
    }

    private void extractTitleFromString(String str)
    {
        String start = "\" alt=\"";
        String end = "\"";
        str = str.substring(str.indexOf(start)+start.length());
        str = str.substring(0, str.indexOf(end));
        Translator translator = new Translator();
        System.out.println("title= "+str);
        System.out.println("translated "+translator.translate(str, Language.JAPANESE, Language.ENGLISH));
        //new JOptionPane().showConfirmDialog(null, "translated "+translator.translate(str, Language.JAPANESE, Language.ENGLISH));
        //System.exit(0);
        itemTitleList.add(str);

    }

    private void extractItemDescriptionUrlFromString(String str)
    {
        String start = "http://ord.yahoo.co.jp/o/auction/SIG";
        String end = "\"";
        str = str.substring(str.indexOf(start));
        str = str.substring(0, str.indexOf(end));
        System.out.println("ItemDescriptionUrl= "+str);
        itemDescriptionUrlList.add(str);

    }

    private void extractSellerUrlFromString(String str)
    {
        String start = "<div class=\"a4 cf\"><p>";
        String start2 = "<a href=\"";
        String end = "\"";
        str = str.substring(str.indexOf(start)+start.length());
        str = str.substring(str.indexOf(start2)+start2.length());
        str = str.substring(0, str.indexOf(end));
        System.out.println("Seller Url= "+str);
        sellerUrlList.add(str);

    }

    private void extractSellerFromString(String str)
    {
        String start = "<div class=\"a4 cf\"><p>";
        String start2 = "\">";
        String end = "</a>";
        str = str.substring(str.indexOf(start)+start.length());
        str = str.substring(str.indexOf(start2)+start2.length());
        str = str.substring(0, str.indexOf(end));
        System.out.println("Seller name= "+str);
        sellerList.add(str);

    }

    private void extractPriceFromString(String str)
    {
        //todo remove yens symbol
        String start = "<td class=\"pr1\">";
        String end = "円";
        str = str.substring(str.indexOf(start)+start.length());
        str = str.substring(0, str.indexOf(end)+end.length());
        System.out.println("Price= "+str);
        itemPriceList.add(str);

    }

    private void extractItemBiddingHistoryUrlFromString(String str)
    {
        String start = "<td class=\"bi\"><a href=\"";
        String end = "\"";
        if (!str.contains(start))
        {
            System.out.println("None");
            itemBiddingHistoryUrlList.add("None");
        }
        else
        {
            str = str.substring(str.indexOf(start)+start.length());
            str = str.substring(0, str.indexOf(end));
            System.out.println("bidding history "+str);
            itemBiddingHistoryUrlList.add(str);
            downloadAndExtractHistory(str);
        }
    }

    private void downloadAndExtractHistory(String url)
    {

        //todo extract info obtenu

        //ItemDescriptionUrlList = new Vector <String>();
        try
        {
            reader = new HtmlSourceReaderToDoc();
            itemHistoryList = reader.readHistory(url);
            for (String auctionHistory:itemHistoryList)
            {
                extractTimeFromString(auctionHistory);//time
                
                //bidder
                //bid type
                //qty
                //amt
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();

        }
    }

    private void extractTimeFromString(String str)
    {
        String start = "<tr><td class=\"decBg01\">[";
        String end = "]";
        String minSymbol = "分";
        String hourSymbol = "時";
        String daySymbol = "日";
        String monthSymbol = "月";
        String tmpStr;
        str = str.substring(str.indexOf(start)+start.length());
        tmpStr = str.substring(0, str.indexOf("��"));
        int month = Integer.parseInt(tmpStr);
        str = str.substring(str.indexOf("��")+"��".length()+1);
        tmpStr = str.substring(0, str.indexOf("��"));
        int day = Integer.parseInt(tmpStr);
        str = str.substring(str.indexOf("��")+"��".length()+1);
        tmpStr = str.substring(0, str.indexOf("��"));
        int hour = Integer.parseInt(tmpStr);
        str = str.substring(str.indexOf("��")+"��".length()+1);
        tmpStr = str.substring(0, str.indexOf("ʬ"));
        int min = Integer.parseInt(tmpStr);
        System.out.println("date/time "+day+"/"+month+" "+hour+":"+min);
        Calendar cal = Calendar.getInstance();
        cal.set(2011, month -1, day, hour, min);
        Date date = new Date(cal.getTimeInMillis());
        System.out.println("sqlDate"+date);
        Time time = new Time(cal.getTimeInMillis());
        System.out.println("sqlTime"+time);
    }

    public static void main(String[] args)
    {
        new urlExtractorFromDoc("http://auctions.search.yahoo.co.jp/search?ei=UTF-8&p=NSR&rkf=1&oq=&auccat=2084008672&slider=0&tab_ex=commerce&s1=bids&o1=a");

    }

}
